package com.eecs393.drymouth;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SubmitLogin extends Activity {
	private JSONObject myJSON;
	private String rawData;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Bundle extras = this.getIntent().getExtras();
		String username = extras.getString("usernamekey");
		String password = extras.getString("passwordkey");
		
		String url = "http://www.smigula.com/drymouth/auth/mobile_login/" + username + "/" + password;
		
		connect(url);
		
		try {
			myJSON = new JSONObject(rawData);
			if(myJSON.getString("login_status").equals("true"))
			{
				Intent search = new Intent(this, ConditionSearch.class);
				search.putExtra("login_status", true);
				search.putExtra("username", myJSON.getString("username"));
				search.putExtra("newLoginKey", true);
				this.startActivity(search);
			}
			else if(myJSON.getString("login_status").equals("false"))
			{
				Intent reSubmit = new Intent(this, EnterLogin.class);
				reSubmit.putExtra("failed_login", true);
				this.startActivity(reSubmit);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    protected void connect(String url)
    {
 
        HttpClient httpclient = new DefaultHttpClient();
 
        // Prepare a request object
        HttpGet httpget = new HttpGet(url); 
 
        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);      
 
            // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
 
            if (entity != null) {
 
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                rawData = convertStreamToString(instream);
//                Log.i("Praeda",result);
          
             // Closing the input stream will trigger connection release
            	instream.close();
            }
        } 
        catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    protected String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}